package com.guanglan.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.guanglan.domain.SysUser;
import com.guanglan.service.SysUserService;
import com.guanglan.utils.PageUtil;

/**
* @ClassName: UserController  
* @Description: TODO(用户管理类)  
* @author GuangLan good
* @date 2017年9月10日  
 */

@RequestMapping("/user")
@Controller
public class UserController {
	
	Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 
	* @Title: userIndex  
	* @Description: TODO(进入用户管理首页)  
	* @param @param userName
	* @param @param userPhone
	* @param @param nowPage
	* @param @param model
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/userIndex")
	public String userIndex(@RequestParam(value="userName",required=false,defaultValue="") String userName,
							@RequestParam(value="userPhone",required=false,defaultValue="") String userPhone,
							@RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,
							Model model){
		
		int start = PageUtil.getStart(nowPage);  //获取到的开始查询索引
		int pageSize = PageUtil.pageSize; //获取页面容量
		Map<String,Object> params = new HashMap<>();
		params.put("userName", userName);
		params.put("userPhone", userPhone);
		params.put("start", start);
		params.put("pageSize", pageSize);
		List<SysUser> users = sysUserService.selectSysUsers(params);
		Integer count = sysUserService.selectSysUsersCount(params);
		int countPage = PageUtil.countPage(count);
		String html = PageUtil.getPanationStringHtml(countPage, nowPage);
		model.addAttribute("html",html);
		model.addAttribute("users", users);
		return "userIndex";
	}
	
	/**
	 * 
	* @Title: addOrUpdateUserPage  
	* @Description: TODO(进入添加和修改页面)  
	* @param @param id
	* @param @param model
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/addOrUpdateUserPage")
	public String addOrUpdateUserPage(@RequestParam(value="id",required=false,defaultValue="") String id,Model model){
		if(StringUtils.trimToNull(id) != null){
			SysUser sysUser = sysUserService.selectByPrimaryKey(Long.valueOf(id));
			model.addAttribute("sysUser", sysUser);
		}
		return "edit";
	}
	
	/**
	 * 
	* @Title: addOrUpdateUser  
	* @Description: TODO(新添加或修改用户)  
	* @param @param userName
	* @param @param userPhone
	* @param @param realName
	* @param @param passWord
	* @param @param email
	* @param @param address
	* @param @param isLock
	* @param @param id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/addOrUpdateUser")
	public String addOrUpdateUser(@RequestParam(value="userName",required=false,defaultValue="") String userName,
								  @RequestParam(value="userPhone",required=false,defaultValue="") String userPhone,
								  @RequestParam(value="realName",required=false,defaultValue="") String realName,
								  @RequestParam(value="passWord",required=false,defaultValue="") String passWord,
								  @RequestParam(value="email",required=false,defaultValue="") String email,
								  @RequestParam(value="address",required=false,defaultValue="") String address,
								  @RequestParam(value="isLock",required=false,defaultValue="1") byte isLock,
								  @RequestParam(value="id",required=false,defaultValue="") String id){
		SysUser sysUser = new SysUser();
		sysUser.setUserName(userName);
		sysUser.setUserPhone(StringUtils.trimToNull(userPhone));
		sysUser.setRealName(StringUtils.trimToNull(realName));
		sysUser.setPassWord(passWord);
		sysUser.setEmail(StringUtils.trimToNull(email));
		sysUser.setBirthday(new Date());
		sysUser.setAddress(StringUtils.trimToNull(address));
		if(StringUtils.trimToNull(id) != null){
			sysUser.setId(Long.valueOf(id));
			sysUser.setIsLock(isLock);
			int count = sysUserService.updateByPrimaryKeySelective(sysUser);
			log.info("更新用户信息{}",count > 0 ? "成功":"失败");
		}else{
			int count = sysUserService.insertSelective(sysUser);
			log.info("新增插入用户信息{}",count > 0 ? "成功":"失败");
		}
		
		return "redirect:userIndex";
		
	}
	
	/**
	 * 
	* @Title: delUser  
	* @Description: TODO(删除该用户)  
	* @param @param id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/delUser")
	public String delUser(@RequestParam(value="id",required=true,defaultValue="") long id){
		int delUserResult = sysUserService.deleteByPrimaryKey(id);
		log.info("删除用户信息{}",delUserResult > 0 ? "成功":"失败");
		return "redirect:userIndex";
	}
	
	

}
